package com.winz.cultureapp.widge;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.winz.cultureapp.R;

/**
 * 自定义PopupWindow
 */
public class CustomPopupWindow extends PopupWindow{

    private View window;


    public CustomPopupWindow(Context context,int resId){
        this(context,resId, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public CustomPopupWindow(Context context, int resId, int width, int height) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        window = inflater.inflate(resId, null);


        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(width);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(height);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);

        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //设置SelectPicPopupWindow的View
        this.setContentView(window);
        window.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    public void dismiss(){
        super.dismiss();
    }

    public void showAsDropDown(View root){
        if(Build.VERSION.SDK_INT >= 24){
            Rect visibleFrame = new Rect();
            root.getGlobalVisibleRect(visibleFrame);
            int height = root.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }
        super.showAsDropDown(root);

    }

    public View getWindow() {
        return window;
    }
}
